/** 2018/2/23 17:42
 *  作者:周志豪
 *  功能:封装请求方法
 *  备注:
 */
 import Bus from "./Bus"
 import axios from 'axios'
 import qs from 'qs'
 import CONFIG from './config'
 import util from "@/assets/js/util"
 import Vue from "vue";
 let isRelogin = true;
 //axios默认参数配置
 axios.defaults.timeout = 1000 * 65;
 //添加一个请求拦截器
 axios.interceptors.request.use(function (config) {
   let authToken = util.getCookie('auth-token');
   config.headers.common['Authorization'] = 'Bearer ' + util.getCookie('auth-token');
  //  config.headers['auth-token'] = (util.getCookie('auth-token') && util.getCookie('auth-token') !== '') ? util.getCookie('auth-token') : '123';
     if (config.apiType === 'notice' || config.apiType === 'form') {
      //  config.data = qs.stringify(config.data);
      config.responseType = 'blob'
      //  config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
     } else {
       config.headers['Content-Type'] = 'application/json;charset=UTF-8';
     }
     if(config.apiType == "cms"){
       config.headers['Cache-Control'] = 'no-cache';
     }
 
   return config;
 }, function (error) {
   return Promise.reject(error);
 });
 //添加一个响应拦截器
 axios.interceptors.response.use(function (res) {
   let result;
   switch (res.status) {
     case 200:
       let data = res.data;
       if(res.headers['content-type'] == 'application/octet-stream;charset=UTF-8'){//文件流下载
         result = res
         return result;
       }
       if(data.rows && data.rows.respCode === "0000"){
         result = data;
       } else if(data.retCode == "0000000" || data.retCode == "2000000"){
         result = data;
       }else {
        //  debugger;
        // this.$message({
        //     message: res.data.retDesc,
        //     type: 'warning'
        // });
        alert(res.data.retDesc);
       }
       break;
   }
   //在这里对返回的数据进行处理
   return result;
 }, function (error) {
   if (error.response) {
     switch (error.response.status) {
       case 401:
         let data = error.response.data;
         util.removeCookie('auth-token'); // 清除cookie登录票据
         Vue.prototype.$alert('很抱歉，您未登录或者登录信息过期，请重新登录！', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            window.location.href ="http://10.168.60.31/problem/#/login";
          }
        });
         break;
       case 404:
         console.log("404:找不到文件");
         break;
       case 500:
         console.log("500:服务器出错");
         break;
     }
   }
   return error;
 });
 
 export default function request(url, option) {
   const httpUrl = url;
   let reqUrl = '';
   if(option.body.method === 'GET'){
     reqUrl = httpUrl + '?' + (option.body.data ? qs.stringify(option.body.data) :'');
   }else{
     reqUrl = httpUrl;
   }
   const defaultOptions = {
     method: 'POST',
     url: reqUrl,
     apiType: option.apiType
   };
   const newOptions = { ...defaultOptions, ...option.body };
   return axios({
     ...newOptions
   }).then(res => {
       return res
   })
 }
 